# -*- coding: utf-8 -*-

# http://git.100credit.cn/tezhDev/featureModelK8sPre/tree/master
import os
os.chdir(r"D:\新建文件夹\众安模型部署_ally_90\解析")
os.listdir()
import joblib
from model_parser.lgb import LGBModelParser



model = joblib.load('za_ally_90.pkl')
final_features = model.booster_.feature_name()



mp = LGBModelParser(model, final_features)
with open('za_ally_90.py', 'w', encoding='utf-8') as f:
    f.write(mp.parse())




# hdfk包解析做了缺失值填充的XGB

import os
os.chdir(r"D:\新建文件夹\代码整理\解析xgb模型结果")
os.listdir()
import pickle
from hdfk.models import XGBModelParser


f = open('model_210810_2.pkl','rb')  
model = pickle.load(f)


# 批量加入入模变量
import re
df_feature = pd.DataFrame(final_features,columns = ['入模原始变量']).sort_values(by = '入模原始变量')
df_feature['类别'] = '画像'
df_feature['入模模块'] = np.nan

var_dict = {
    'tl': 'TotalLoan-V2.0',
    'ae': 'ApplyEvaluate-V1.0',
    'pd': 'PopulationDerivation-V1.0',
    'alf':'ApplyFeature-V3.0',
    'mma':'MultipleModelA-V1.0',
    'stab':'Stability_c-V2.0',
    'als':'ApplyLoanStr-V2.0',
    'ir':'InfoRelation-V3.0',
    'cf':'ConsumptionFeature-V1.0',
    'alu':'ApplyLoanUsury-V1.1',
    'gl':'GrayListExpand-V1.0',
    'mmb':'MultipleModelB-V1.0',
    'ql':'QuantileLevel-V1.0',
    'tl':'TotalLoan-V2.0',
    'frg':'FraudRelation_g-V1.0',
    'aes':'ApplyEvaluateStr-V1.0'
}

flag_dict = {
    'tl': 'flag_totalloan',
    'ae': 'flag_applyevaluate',
    'pd': 'flag_populationderivation',
    'alf':'flag_ApplyFeature',
    'mma':'flag_multiplemodela',
    'stab':'flag_stability_c',
    'als':'flag_applyloanstr',
    'ir':'flag_inforelation',
    'cf':'flag_ConsumptionFeature',
    'alu':'flag_applyloanusury',
    'gl':'flag_graylistexpand',
    'mmb':'flag_multiplemodelb',
    'ql':'flag_quantilelevel',
    'tl':'flag_totalloan',
    'frg':'flag_fraudrelation_g',
    'aes':'flag_applyevaluatestr'
}

product_list = list(set([i[0:i.find('_')] for i in df_feature['入模原始变量'].tolist()]))

for i in product_list:
    temp_var = [t for t in df_feature['入模原始变量'].tolist() if re.match(i,t)]
    df_feature.loc[df_feature['入模原始变量'].isin(temp_var),'入模模块'] = var_dict[i]
    df_feature = df_feature.append([{'入模原始变量':flag_dict[i],'类别':'画像','入模模块':var_dict[i]}], ignore_index=True)
